<?php
require_once dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';
include dirname(dirname(__FILE__)).'/duty-table-manage/functions.php';
include_once dirname(dirname(__FILE__)).'/user-manage/functions.php';

function getExchangeByToUserId($id) {
	$conn = get_db_conn();
	$sql = 'select * from exchange_duty where to_user_id = '.$id.' and status = "pending" limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	if(mysql_num_rows($result)==0)return NULL;
	$ret = mysql_fetch_array($result);
	return $ret;
}

function getExchangeByFromUserId($id) {
	$conn = get_db_conn();
	$sql = 'select * from exchange_duty where from_user_id = '.$id.' and status = "pending" limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	if(mysql_num_rows($result)==0)return NULL;
	$ret = mysql_fetch_array($result);
	return $ret;
}

function getCurDutyTableId() {
	$conn = get_db_conn();
	$sql = 'select id from cur_duty_table where is_on_use = 1 limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	if(mysql_num_rows($result)==0) {
		$next_table = getDutyTable();
		$time = mktime(0,0,0,date('m'),date('d')+1,date('y'));
		$start_time = date('Y-m-d',$time);
		$update_time = date('Y-m-d H:i:s');
		$sql = 'insert into cur_duty_table(table_content,update_time,start_time,is_on_use) 
		values(\''.json_encode($next_table).'\',"'.$update_time.'","'.$start_time.'",1);';
		mysql_query($sql,$conn) or die("db error");
		$sql = 'select id from cur_duty_table where is_on_use = 1 limit 0,1;';
		$result = mysql_query($sql,$conn) or die("db error");
	}
	$row = mysql_fetch_array($result);
	return $row['id'];
}

function getCurDutyTable() {
	$conn = get_db_conn();
	$id = getCurDutyTableId();
	$sql = 'select * from cur_duty_table where id = '.$id.' limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	$ret = json_decode($row['table_content'],true);
	return $ret;
}

function getCurDutyTableUpdateTime() {
	$conn = get_db_conn();
	$id = getCurDutyTableId();
	$sql = 'select update_time from cur_duty_table where id = '.$id.' limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	return $row['update_time'];
}

function getCurDutyTableStartTime() {
	$conn = get_db_conn();
	$id = getCurDutyTableId();
	$sql = 'select start_time from cur_duty_table where id = '.$id.' limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	return $row['start_time'];
}

function updateCurDutyTable($table) {
	$sql = "";
	$id = getCurDutyTableId();
	$sql.= 'update cur_duty_table set table_content = \''.$table.'\',update_time = "'.date('Y-m-d H:i:s').'" where id = '.$id.';';
	$conn = get_db_conn();
	mysql_query($sql,$conn) or die("db error");
	return true;
}

function getCurDutyUser() {
	$conn = get_db_conn();
	$sql = 'select id,user_real_name from user where is_user_on_duty = 1;';
	$result = mysql_query($sql,$conn) or die("db error");
	$ret = array();
	$cnt = 0;
	while($row = mysql_fetch_array($result)) {
		$ret[$row['id']]=$row['user_real_name'];
	}
	return $ret;
}

function generateNextWeekDutyTable() {
	$conn = get_db_conn();
	$id = getCurDutyTableId();
	$next_table = getDutyTable();
	$time = mktime(0,0,0,date('m'),date('d')+1,date('y'));
	$start_time = date('Y-m-d',$time);
	$update_time = date('Y-m-d H:i:s');
	$sql = 'update cur_duty_table set is_on_use = 0,end_time = \''.$start_time.'\' where id = '.$id.';';
	mysql_query($sql,$conn) or die("db error");
	$sql = 'insert into cur_duty_table(table_content,update_time,start_time,is_on_use) 
	values(\''.json_encode($next_table).'\',"'.$update_time.'","'.$start_time.'",1);';
	mysql_query($sql,$conn) or die("db error");
	return true;
}

function clearExchangeStatus($user_id,$exchange_id) {
	$conn = get_db_conn();
	$cur_user = getUserById($user_id);
	if($cur_user['exchange_status']=='3') {
		$sql = 'update exchange_duty set status = "accepted" where id = '.$exchange_id.';';
		mysql_query($sql,$conn) or die("db error");
	} else if($cur_user['exchange_status']=='4') {
		$sql = 'update exchange_duty set status = "rejected" where id = '.$exchange_id.';';
		mysql_query($sql,$conn) or die("db error");
	}
	$sql = 'update user set exchange_status = 0 where id = '.$user_id.';';
	mysql_query($sql,$conn) or die("db error");
	return true;
}

function getStartEndTimeList() {
	$conn = get_db_conn();
	$sql = 'select start_time,end_time,id from cur_duty_table;';
	$result = mysql_query($sql,$conn) or die("db error");
	$ret = array();
	while($row = mysql_fetch_array($result)) {
		$ret[$row['id']]=$row['start_time'].'~';
		if($row['end_time']!='')$ret[$row['id']].=$row['end_time'];
		else $ret[$row['id']].='本周结束';
	}
	ksort($ret);
	return $ret;
}

function getDutyTableById($id) {
	$conn = get_db_conn();
	$sql = 'select * from cur_duty_table where id = '.$id.' limit 0,1;';
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	return $row;
}